package com.xml.sax;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.helpers.XMLReaderFactory;

import java.io.*;

/**
 * 使用SAXParse解析xml文档
 * 运行条件： 保证saxDemo.xml文件   文件路径为saxDemo.xml文件路径
 *
 *  源码解析：
 *      createXMLReader有一定规则  本机默认加载com.sun.org.apache.xerces.internal.parsers.SAXParser类
 *
 * <p>
 * Created by yangliwei on 2018/3/16.
 */
public class SAXParseDemo extends DefaultHandler{
    public static void main(String[] args) {
        XMLReader xr = null;
        try {
            xr = XMLReaderFactory.createXMLReader();
            SAXParseDemo handler = new SAXParseDemo();
            xr.setContentHandler(handler);
            xr.setErrorHandler(handler);
            xr.parse(new InputSource(new FileReader(new File("D:\\ideaProject\\javaStudy\\src\\com\\xml\\sax\\saxDemo.xml"))));
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    ////////////////////////////////////////////////////////////////////
    // Event handlers.
    ////////////////////////////////////////////////////////////////////


    @Override
    public void startDocument ()
    {
        System.out.println("Start document");
    }


    @Override
    public void endDocument ()
    {
        System.out.println("End document");
    }


    @Override
    public void startElement (String uri, String name,
                              String qName, Attributes atts)
    {
        if ("".equals (uri)) {
            System.out.println("Start element: " + qName);
        } else {
            System.out.println("Start element: {" + uri + "}" + name);
        }
    }


    @Override
    public void endElement (String uri, String name, String qName)
    {
        if ("".equals (uri)) {
            System.out.println("End element: " + qName);
        } else {
            System.out.println("End element:   {" + uri + "}" + name);
        }
    }


    @Override
    public void characters (char ch[], int start, int length)
    {
        System.out.print("Characters:    \"");
        for (int i = start; i < start + length; i++) {
            switch (ch[i]) {
                case '\\':
                    System.out.print("\\\\");
                    break;
                case '"':
                    System.out.print("\\\"");
                    break;
                case '\n':
                    System.out.print("\\n");
                    break;
                case '\r':
                    System.out.print("\\r");
                    break;
                case '\t':
                    System.out.print("\\t");
                    break;
                default:
                    System.out.print(ch[i]);
                    break;
            }
        }
        System.out.print("\"\n");
    }

    public SAXParseDemo() {
        super();
    }
}

